package com.java1234.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.java1234.model.Article;
import com.java1234.model.PageBean;
import com.java1234.service.ArticleService;
import com.java1234.util.ResponseUtil;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/article")
public class ArticleController {
	@Resource
	private ArticleService articleService;
	@RequestMapping("/save")
	public String save(Article article,HttpServletResponse response)throws Exception{
		
		int resultTotal=0; // �����ļ�¼����
		if(article.getId()==null){
			resultTotal=articleService.add(article);
		}else{
			resultTotal=articleService.update(article);
		}
		JSONObject result=new JSONObject();
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(result,response);
		return null;
	}
	@RequestMapping("/list")
	public String list(HttpServletResponse response)throws Exception{
		List<Article> list=articleService.articleList();
		JSONArray result=new JSONArray();
		result.add(list);
		ResponseUtil.write(result,response);
		return null;
	}
	
	@RequestMapping("/manageList")
	public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Article> articleList=articleService.manageList(map);
		Long total=articleService.getTotal();
		JSONObject result=new JSONObject();
		JSONArray jsonArray=JSONArray.fromObject(articleList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(result,response);
		
		return null;
	}
	
	//ɾ������
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{
		String []idsStr=ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			articleService.delete(Integer.parseInt(idsStr[i]));
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(result,response);
		return null;
	}
	
	//ͨ��ID���������Ϣ
	@RequestMapping("/findById")
	public String findById(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{
		Article article=articleService.findById(Integer.parseInt(id));
		JSONObject jsonObject=JSONObject.fromObject(article);
		ResponseUtil.write(jsonObject,response);
		return null;
	}
	//��õ���������ϸ����
	@RequestMapping("/details/{id}")
	public ModelAndView details(@PathVariable("id") Integer id,HttpServletResponse response)throws Exception{
		//System.out.println("����õ���ϸ��Ϣ");
		ModelAndView mav=new ModelAndView();
		//System.out.println("1");
		Article article=articleService.findById(id);
		//System.out.println(article.getContent());
		//System.out.println("2");
		mav.addObject("blog", article);
		//System.out.println("3");
		mav.setViewName("detail");
		//System.out.println("��ͼ�������ú�");
		return mav;
	}

}
